package com.irishud.hudnavi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.navi.model.AMapTrafficStatus;
import com.irishud.hudnavi.custom_view.TrafficStatusBarView;
import com.irishud.protocol.NaviUpdateInfo;
import com.irishud.protocol.TrafficStatusUpdateData;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class x3SimpleNaviActivity extends Activity {
    @Bind(R.id.txt_next_road_name)
    TextView txtNextRoadName;
    @Bind(R.id.txt_next_road_distance)
    TextView txtNextRoadDistance;
    @Bind(R.id.top_area)
    RelativeLayout topArea;
    @Bind(R.id.txt_current_speed)
    TextView txtCurrentSpeed;
    @Bind(R.id.txt_speed_unit)
    TextView txtSpeedUnit;
    @Bind(R.id.txt_current_speed_area)
    LinearLayout txtCurrentSpeedArea;
    @Bind(R.id.txt_current_road_name)
    TextView txtCurrentRoadName;
    @Bind(R.id.left_part)
    RelativeLayout leftPart;
    @Bind(R.id.img_navi_icon_type)
    ImageView imgNaviIconType;
    @Bind(R.id.middle_part)
    RelativeLayout middlePart;
    @Bind(R.id.camera_type_image)
    ImageButton cameraTypeImage;
    @Bind(R.id.camera_distance_bg)
    ImageView cameraDistanceBg;
    @Bind(R.id.camera_distance)
    TextView cameraDistance;
    @Bind(R.id.camera_distance_area)
    RelativeLayout cameraDistanceArea;
    @Bind(R.id.monitor_camera_area)
    RelativeLayout monitorCameraArea;
    @Bind(R.id.img_speed_limit)
    ImageView imgSpeedLimit;
    @Bind(R.id.speed_limit_area)
    RelativeLayout speedLimitArea;
    @Bind(R.id.right_part)
    RelativeLayout rightPart;
    @Bind(R.id.content_area)
    RelativeLayout contentArea;
    @Bind(R.id.traffic_status_bar)
    TrafficStatusBarView trafficStatusBar;
    @Bind(R.id.bottom_area)
    RelativeLayout bottomArea;
    @Bind(R.id.speed_limit_value)
    TextView speedLimitValue;
    @Bind(R.id.camera_part)
    LinearLayout cameraPart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x3_activity_fullscreen);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        speedLimitValue.setText("--");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
    }

    private int getIconType(Context context, int type) {
        String strId = "icon_type_" + type;
        return getDrawableId(context, strId);
    }

    private int getDrawableId(Context paramContext, String paramString) {
        return paramContext.getResources().getIdentifier(paramString, "drawable", paramContext.getPackageName());
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleNaviUpdate(NaviUpdateInfo naviUpdateInfo) {
        // 当前道路信息
        txtCurrentRoadName.setText(naviUpdateInfo.currentRoadName);
        // 下个路口信息
        txtNextRoadName.setText(naviUpdateInfo.nextRoadName);
        txtNextRoadDistance.setText(NaviUtil.getFriendlyLength(naviUpdateInfo.nextRoadDistance));

        // 导航转向箭头
        imgNaviIconType.setBackgroundResource(getIconType(this,naviUpdateInfo.nextTurnIcon));

        // 摄像头信息
        speedLimitArea.setVisibility(View.GONE);
        monitorCameraArea.setVisibility(View.GONE);
        if (naviUpdateInfo.cameraType == 0) {// 测速摄像
            speedLimitArea.setVisibility(View.VISIBLE);
            speedLimitValue.setText(String.valueOf(naviUpdateInfo.speedLimit));
        }

        if (naviUpdateInfo.cameraType == 1) {// 监控摄像
            monitorCameraArea.setVisibility(View.VISIBLE);
            cameraDistance.setText(String.valueOf(naviUpdateInfo.cameraDistance));
        }
        // 当前车速
        txtCurrentSpeed.setText(String.valueOf(naviUpdateInfo.speed));
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleTrafficStatusDataUpdate(TrafficStatusUpdateData trafficStatus){

    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleX3DemoObdEvent(x3obdDemoEvent event){
        if(event.getEvent() == x3obdDemoEvent.Open){
            Intent intent = new Intent(this,x3obdDemoActivity.class);
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleTrafficStatus(TrafficStatusUpdateData data){
        List<TrafficStatusBarView.BarItem> list = new ArrayList<>();
        for(AMapTrafficStatus status : data.trafficStatus){
            list.add(new TrafficStatusBarView.BarItem(status.getStatus(),status.getLength()));
        }
        trafficStatusBar.setTotalLen(data.total);
        trafficStatusBar.setFinish_progress(data.total - data.remaining);
        trafficStatusBar.updateBarItemList(list);
    }
}
